﻿
$c.fn.tabs = function (config) {
    var _this = $(this.jo);
    var obj = function () {
        this.config = $.extendEx({}, this.defConfig, config);
    }
    obj.prototype.tabs = function () {
        this.create();
    }
    obj.prototype.getTab = function (id) {
        var result = null;
        $.each(_this.find('.kui-tabs-title'), function (i, o) {
            if (o.id == id) {
                //$(o).addClass('kui-selected');
                result = { titlePart: $(o) };
            }
            //else {
            //    $(o).removeClass('kui-selected');
            //}
        });
        $.each(_this.find('.kui-tabs-content'), function (i, o) {
            if (o.id == id) {
                //$(o).addClass('kui-selected');
                result.contentPart = $(o);
            }
            //else {
            //    $(o).removeClass('kui-selected');
            //}
        });
        return result;
    }
    obj.prototype.update = function (id,tab) {
        var result = _obj.getTab(id);
        if (!result) {
            return;
        }
        var titlePart = result.titlePart;
        var contentPart = result.contentPart;
        if(tab.title){
            titlePart.find('span').html(tab.title);
        }
        if (!tab.content) {
            tab.content = '';
        }
        if ($.isFunction(tab.content)) {
            contentPart.html(tab.content());
        }else if (tab.content.indexOf('url:') == 0) {
            var loadingIndex = 0;
            if (tab.showLoading == true && window.kwindow) {
                loadingIndex = kwindow.loading(tab.loadingConfig);
            }
            content = '<iframe scrolling="' + (tab.scrolling || "auto") + '" allowtransparency="true" class="kui-tabs-iframe" frameborder="0" onload="javascript:if(' + loadingIndex + '>0){kwindow.close(' + loadingIndex + ');}" src="' + tab.content.substr(4) + '"></iframe>'
            tab.style = $.extendEx({}, { overflow: 'hidden' }, tab.style);
            contentPart.html(content);
        }
        else if (tab.content.indexOf('ajax-get:') == 0) {
            var url = tab.content.substr(9);
            var loadingIndex = 0;
            if (tab.showLoading == true) {
                loadingIndex = kwindow.loading(tab.loadingConfig);
            }
            $.get(url, function (data, status) {
                kwindow.close(loadingIndex);
                contentPart.html(data);
            });
        }
        else if (tab.content.indexOf('ajax-post:') == 0) {
            var url = tab.content.substr(10);
            var loadingIndex = 0;
            if (tab.showLoading == true) {
                loadingIndex = kwindow.loading(tab.loadingConfig);
            }
            $.post(url, {}, function (data, status) {
                kwindow.close(loadingIndex);
                contentPart.html(data);
            });
        }
        else {
            contentPart.html(tab.content);
        }
    }
    obj.prototype.select = function (id) {
        var _obj = this;
        var result = _obj.getTab(id);
        if (!result) {
            return result;
        }
        _this.find('.kui-tabs-title,.kui-tabs-content').removeClass('kui-selected');
        result.titlePart.addClass('kui-selected');
        result.contentPart.addClass('kui-selected');

        //$.each(_this.find('.kui-tabs-title'), function (i, o) {
        //    if (o.id == id) {
        //        $(o).addClass('kui-selected');
        //        result = {titlePart:$(o)};
        //    }
        //    else {
        //        $(o).removeClass('kui-selected');
        //    }
        //});
        //$.each(_this.find('.kui-tabs-content'), function (i, o) {
        //    if (o.id == id) {
        //        $(o).addClass('kui-selected');
        //        result.contentPart = $(o);
        //    }
        //    else {
        //        $(o).removeClass('kui-selected');
        //    }
        //});
        return result;
    }

    obj.prototype.add=function(tab){
        var _obj = this;
        var headPart = _this.find('.kui-tabs-head');
        var bodyPart = _this.find('.kui-tabs-body');
        var titlePart = $('<span class="kui-tabs-title"></span>').appendTo(headPart);
        var textPart = $('<span>' + tab.title + '</span>').appendTo(titlePart);
        var closePart = $('<i class="kui-icon iconfont icon-close"></i>').appendTo(titlePart);
        var contentPart = $('<div class="kui-tabs-content"></div>').appendTo(bodyPart);
        titlePart[0].id = tab.id;
        contentPart[0].id = tab.id;
        titlePart.on(_obj.config.switchEvent, function () {
            //_this.find('.kui-tabs-title').removeClass('kui-selected');
            //_this.find('.kui-tabs-content').removeClass('kui-selected');
            //titlePart.addClass('kui-selected');
            //contentPart.addClass('kui-selected');
            _obj.select(tab.id);
        });
        closePart.click(function () {
            titlePart.remove();
            contentPart.remove();
        });
        
        _obj.update(tab.id, tab);

        titlePart.setStyle($.extendEx({}, _obj.config.titlePartStyle, tab.titlePartStyle));
        textPart.setStyle($.extendEx({}, _obj.config.textPartStyle, tab.textPartStyle));
        closePart.setStyle($.extendEx({}, _obj.config.closePartStyle, tab.closePartStyle));
        contentPart.setStyle($.extendEx({}, _obj.config.contentPartStyle, tab.contentPartStyle));

        headPart.find('.kui-tabs-head-clear').remove();
        headPart.append('<div class="kui-tabs-head-clear" style="clear:both; font-size:0px; height:0px; overflow:hidden;"></div>');
    }
    obj.prototype.create = function () {
        _this.addClass('kui-tabs');
        var _obj = this;
        var headPart = $('<div class="kui-tabs-head"></div>').appendTo(_this);
        var bodyPart = $('<div class="kui-tabs-body"></div>').appendTo(_this);
        $.each(this.config.tabs, function (i, tab) {
            _obj.add(tab);
        });
        //_this.find('.kui-tabs-title:eq(0)').addClass('kui-selected');
        //_this.find('.kui-tabs-content:eq(0)').addClass('kui-selected');
        if (_obj.config.tabs.length > 0) {
            _obj.select(_obj.config.tabs[0].id);
        }
        
        _this.setStyle(this.config.style);
        headPart.setStyle(this.config.headPartStyle);
        bodyPart.setStyle(this.config.bodyPartStyle);
    }

    obj.prototype.defConfig = {
        switchEvent:'click'
    }

    obj.prototype._autoHeight = function () {
        var headHeight = _this.find('.kui-tabs-head').height();
        //alert(headHeight);
        _this.find('.kui-tabs-body').height(_this.height() - headHeight);;
    }

    var _obj = new obj();
    _obj.tabs();
    ////$(function () {
        _obj._autoHeight();
    //});
    
    $(window).resize(function () {
        _obj._autoHeight();
        //alert('autoheight')
    });
    return _obj;
};